//
//  NSString+sha256.m
//  HSEDU
//
//  Created by oceans on 2018/7/13.
//  Copyright © 2018年 xvv. All rights reserved.
//

#import "NSString+sha256.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (sha256)

- (NSString *)sha256 {
    
    const char* str = [self UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(str, (CC_LONG)strlen(str), result);
            
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
            {
        [ret appendFormat:@"%02x",result[i]];
    }
    //    ret = (NSMutableString *)[ret uppercaseString];
    return ret;
    
//    const char *s = [self cStringUsingEncoding:NSASCIIStringEncoding];
//    NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
//    
//    uint8_t digest[CC_SHA256_DIGEST_LENGTH] = {0};
//    CC_SHA256(keyData.bytes, (CC_LONG)keyData.length, digest);
//    NSData *out = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
//    NSString *hash = [out description];
//    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
//    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
//    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
//    return hash;
}
@end
